package com.black.knight.chess.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.black.knight.chess.R;
import com.black.knight.chess.domain.Move;
import com.black.knight.chess.model.SahModel;

public class MoveAdapter extends BaseAdapter {
    private Context mContext;

    public MoveAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return SahModel.getInstance().getGame().getMoves().size();
    }

    public Move getItem(int position) {
        return SahModel.getInstance().getGame().getMoves().get(position);
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        
    	LinearLayout viewGroup = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.move, parent, false);
    	
        if(SahModel.getInstance().getGame().getSelectedIndex() == 0) return viewGroup;
        
        Move move = SahModel.getInstance().getGame().getMoves().get(position);

        /*ImageView imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(48, 100));
        imageView.setScaleType(ImageView.ScaleType.CENTER);
        imageView.setPadding(0, 0, 0, 5);
        
        ImageView imageView1 = new ImageView(mContext);
        imageView1.setLayoutParams(new GridView.LayoutParams(60, 100));
        imageView1.setScaleType(ImageView.ScaleType.CENTER);
        imageView1.setPadding(0, 0, 0, 5);
        
        ImageView imageView2 = new ImageView(mContext);
        imageView2.setLayoutParams(new GridView.LayoutParams(42, 50));
        imageView2.setScaleType(ImageView.ScaleType.CENTER);
        imageView2.setPadding(0, 0, 0, 0);
        
        imageView.setImageResource(move.getMovedFigure());
        
        if(move.getDestroyedFigure() != R.drawable.empty) {
        	imageView1.setImageResource(move.getDestroyedFigure());
        } else {
        	imageView1.setImageResource(R.drawable.selector);
        }
        
        imageView2.setImageResource(R.drawable.redo);
        
        TextView textView = new TextView(mContext);
        textView.setText("- " + move.fromField());
        textView.setPadding(0, 30, 0, 0);
        textView.setTextSize(12);
        textView.setWidth(40);
        textView.setTextAppearance(mContext, R.style.BoldSerif);  
        textView.setTextColor(mContext.getResources().getColor(R.color.chess));
        
        TextView textView1 = new TextView(mContext);
        textView1.setText(" - " + move.toField());
        textView1.setPadding(0, 30, 0, 0);
        textView1.setTextSize(12);
        textView1.setWidth(40);
        textView1.setTextAppearance(mContext, R.style.BoldSerif);  
        textView1.setTextColor(mContext.getResources().getColor(R.color.chess));
        
        TextView textView2 = new TextView(mContext);
        textView2.setText(mContext.getResources().getString(R.string.move) + ": " + String.valueOf((position + 1)));
        textView2.setPadding(0, 0, 0, 0);
        textView2.setTextSize(11);
        textView2.setGravity(Gravity.CENTER_HORIZONTAL);
        textView2.setTextAppearance(mContext, R.style.BoldSerif);  
        textView2.setTextColor(mContext.getResources().getColor(R.color.chess));
        
        viewGroup = new LinearLayout(mContext);
        viewGroup.setMinimumWidth(parent.getWidth() - 10);
        viewGroup.setGravity(Gravity.CENTER_HORIZONTAL);
        
        LinearLayout moveLayout = new LinearLayout(mContext);
        moveLayout.setOrientation(LinearLayout.VERTICAL);
        moveLayout.setGravity(Gravity.CENTER_HORIZONTAL);
        moveLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        
        LinearLayout moveLayout1 = new LinearLayout(mContext);
        moveLayout1.setOrientation(LinearLayout.HORIZONTAL);
        moveLayout1.setGravity(Gravity.CENTER);
        moveLayout1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        
        LinearLayout moveLayout2 = new LinearLayout(mContext);
        moveLayout2.setOrientation(LinearLayout.HORIZONTAL);
        moveLayout2.setGravity(Gravity.CENTER);
        moveLayout2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        
        moveLayout.addView(moveLayout1);
        moveLayout.addView(moveLayout2);
        
        moveLayout1.addView(textView);
        moveLayout1.addView(imageView2);
        moveLayout1.addView(textView1);
        
        moveLayout2.addView(textView2);
        
        viewGroup.addView(imageView);
        viewGroup.addView(moveLayout);
        viewGroup.addView(imageView1);*/
        
        ImageView movedFigure = (ImageView)viewGroup.findViewById(R.id.movedFigure);
        movedFigure.setImageResource(move.getMovedFigure());
        
        return viewGroup;
    }
}